<%# locals: (thread:, story: nil, ribbon: nil, read_by_notifications: [], show_folder_control: true) -%>
<% previous_depth = nil %>
<% top_comment_depth = nil %>
<%# open list of threads %>
<ol class="comments">
<% thread.each do |comment| %>

    <% if previous_depth %>
      <% if comment.depth > previous_depth %>
        <%# open tree of replies to the previous comment %>
        <ol class="comments">
      <% else %>
        <%# previous comment is the deepest comment in its subtree,
            so it has no replies, only an empty list of replies %>
        <ol class="comments"></ol>

        <%# close tree of replies %>
        <% (previous_depth - comment.depth).times do %>
            </li>
          </ol>
        <% end %>

        <%# close previous subtree %>
        </li>
      <% end %>
    <% end %>
    <% previous_depth = comment.depth %>
    <% top_comment_depth ||= comment.depth %>

    <%# open current subtree %>
    <li class="comments_subtree">

    <%
      force_open = true
      show_tree_lines = true
      # show "on: <title>" for comments from merged stories + on non-story pages
      show_story ||= story.try(:id) != comment.story_id
      # show merge icon only on story page when merged
      was_merged = story && (story.id != comment.story_id)
      is_unread = ribbon&.is_unread?(comment) && @read_by_notifications.exclude?(comment.id)
    %>

<%#heinous_inline_partial(comments/_comment.html.erb)%>
<%# Do not edit, the content before /heinous_inline_partial comes from the named partial %>
<%# locals: (comment:, force_open: false, show_tree_lines: false, show_story: false, was_merged: false, is_unread: false, show_folder_control: true) -%>
<%
  # partial inputs:
  # force_open: this comment is allowed to collapse
  # show_tree_lines: render collapse button (misnamed?)
  # show_story: show "on: story title" end of byline
  # was_merged: show merged icon start of byline
  # is_unread: show (unread) in byline
  # show_folder_control: whether to display the control to hide the comment
%>
<input id="comment_folder_<%= comment.short_id %>"
  class="comment_folder_button" type="checkbox"
  <%= force_open ||
      @user.try(:is_moderator?) ||
      (comment.score > Comment::COLLAPSE_SCORE && !comment.current_flagged?) ? "" : "checked" %>>

      <% can_flag = @user && @user.can_flag?(comment) %>
      <% score_display = comment.score_for_user(@user) %>
      <% draw_tree_line = comment.reply_count > 0 %>

      <div class="
        <%= draw_tree_line ? "comment_parent_tree_line" : "" %>
        <%= can_flag ? "can_flag" : "" %>
        <%= score_display != "&nbsp;" ? "score_shown" : "" %>
      "></div>
<div id="c_<%= comment.short_id %>"
  data-shortid="<%= comment.short_id if comment.persisted? %>"
  class="comment
    <%= comment.current_upvoted? ? "upvoted" : "" %>
    <%= comment.current_flagged? ? "flagged" : "" %>
    <%= comment.score < Comment::SCORE_RANGE_TO_HIDE.first ? "bad" : "" %>
  ">

  <div class="voters">
    <% if show_folder_control %>
      <label for="comment_folder_<%= comment.short_id %>" class="comment_folder"></label>
    <% end %>
    <% if !comment.is_gone? %>
      <%= link_to comment.score_for_user(@user), (@user ? '': login_path), :class => "upvoter" %>
    <% elsif @user&.is_moderator? %>
      <span class="upvoter"><%= comment.score_for_user(@user) %></span>
    <% end %>
  </div>

  <div class="details">
    <div class="byline">
      <a name="c_<%= comment.short_id %>"></a>

      <% if was_merged %>
        <span class="merge"></span>
      <% end %>

      <span class="<%= @showing_user == comment.user ? "showing-user-comment" : nil %>">
        <%= inline_avatar_for @user, comment.user %>
        <%= styled_user_link comment.user, comment %>

        <% if comment.hat %>
          <%= comment.hat.to_html_label %>
        <% end %>

        <% if comment.previewing %>
          previewed
          just now
        <% else %>
          <% if comment.has_been_edited? %>
            edited
          <% elsif comment.is_from_email? %>
            e-mailed
          <% end %>
          <% if comment.previewing %>
            <%= how_long_ago_label(comment.last_edited_at) %>
          <% else %>
            <%= how_long_ago_link(comment_short_id_path(comment), comment.last_edited_at) %>
          <% end %>
          <%= raw "<span class='comment_unread'>(unread)</span>" if is_unread %>
        <% end %>
      </span>

      <% if !comment.previewing %>
        <% if comment.is_editable_by_user?(@user) %>
          |
          <%= link_to 'edit', edit_comment_path(comment), class: :comment_editor %>
        <% end %>

        <% if comment.is_gone? && comment.is_undeletable_by_user?(@user) %>
          |
          <a class="comment_undeletor" href="#c_<%= comment.short_id %>">undelete</a>
        <% elsif !comment.is_gone? && comment.is_deletable_by_user?(@user) %>
          |
          <% if @user && @user.is_moderator? && @user.id != comment.user_id %>
            <a class="comment_moderator" href="#c_<%= comment.short_id %>">delete</a>
          <% else %>
            <a class="comment_deletor" href="#c_<%= comment.short_id %>">delete</a>
          <% end %>
        <% elsif !comment.is_gone? && comment.is_disownable_by_user?(@user) %>
          |
          <%= link_post("disown", disown_comment_path(comment.short_id),
          params: { type: 'comment', show_story: show_story, show_tree_lines: show_tree_lines })
          %>
        <% end %>

        <% if can_flag && !comment.current_flagged? %>
          <span class="dropdown_parent">
            | <a class="flagger">flag</a>
          </span>
        <% elsif comment.current_flagged? %>
          | <a class="flagger">unflag</a>
        <% else %>
          <span class="flagger flagger_stub"></span>
        <% end %>

        <% if @user && !comment.is_gone? && comment.story.accepting_comments? %>
            |
            <a class="comment_replier" unselectable="on" href="<%= reply_comment_path(comment) %>">reply</a>
        <% end %>

        <% if show_story %>
          | on:
          <a href="<%= Routes.title_path comment.story %>"><%= comment.story.title %></a>
        <% end %>

        <span class="reason">
          <% if comment.flags > 0 && comment.show_score_to_user?(@user) %>
            <%# mods shouldn't see usernames for people flagging them %>
            <% if comment.user_id == @user&.id %>
              | <%= comment.vote_summary_for_user %>
            <% elsif @user&.is_moderator? %>
              | <%= comment.vote_summary_for_moderator %>
            <% end %>
          <% elsif comment.current_flagged? %>
            | -1
            <%= Vote::ALL_COMMENT_REASONS[comment.current_vote[:reason]].downcase %>
          <% end %>
        </span>
      <% end %>
    </div>
    <div role="heading" aria-level="3" class="comment_text">
      <% if comment.is_gone? %>
        <p>
        <span class="na">
          [<%= comment.gone_text %><% if @user && @user.is_moderator? %> Visible to moderator:<% end %>]
        </span>
        </p>
        <% if @user && @user.is_moderator? %>
          <%= raw comment.markeddown_comment %>
        <% end %>
      <% else %>
        <%= raw comment.markeddown_comment %>
      <% end %>
    </div>
  </div>
</div>

<%#/heinous_inline_partial(comments/_comment.html.erb)%>

    <% if comment.depth != top_comment_depth %>
      <div class="comment_siblings_tree_line"></div>
    <% end %>

<% end %>

<% if top_comment_depth %>

  <%# previous comment is the deepest comment in its subtree,
      so it has no replies, only an empty list of replies %>
  <ol class="comments"></ol>

  <%# close tree of replies %>
  <% (previous_depth - top_comment_depth).times do %>
      </li>
    </ol>
  <% end %>

  <%# close top subtree %>
  </li>
<% end %>

<%# close list of threads %>
</ol>
